<?php
namespace app\controller\admin;

use app\base\BaseEncryptController;
use app\model\user\UserAddressModel;
use think\Request;
use app\libs\Util;
use think\facade\Config;
/**
 * User
 * Class User
 * @package app\controller
 */
class UserAddress extends BaseEncryptController
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new UserAddressModel();
    }

    /**
     * 分页列表
     * @param string $keyword
     * @param int $page
     * @param int $limit
     * @return \think\response\Json
     */
    public function getPage($user_id='',$page=1,$limit=10)
    {	
        $data = $this->model->getPageData($user_id,$page,$limit);
        return self::returnMsg(1,[
            'totalCount'=>$data['total'],
            'currentPage'=>$data['current'],
            'pageList'=>$data['dataList']
        ]);
    }

    
    /**
     * 用户地址列表
     * @param request
     * @return \think\response\Json
     */
    public function UserAddressList(Request $request)
    {   
        $userInfo=$this->currentUser;//登录信息
        $data = $this->model
        ->where("user_id",$userInfo['user_id'])
        ->order("is_default asc,id desc")
        ->field("id,take_phone,take_name,province,city,district,detail_address,is_default")
        ->select();
        return self::returnMsg(1,$data);
    }

    /**
     * 用户地址编辑详情
     * @param request
     * @return \think\response\Json
     */
    public function UserAddressOne(Request $request)
    {   
        $userInfo=$this->currentUser;//登录信息
        $data = $this->model
        ->where("id",$request->param("id"))
        ->where("user_id",$userInfo['user_id'])
        ->order("is_default asc,id desc")
        ->field("id,take_phone,take_name,province,city,district,detail_address,is_default")
        ->find();
        $data['is_default']=$data['is_default']==1?true:false;
        return self::returnMsg(1,$data);
    }

    /**
     * 删除
     * @param $id
     * @return \think\response\Json
     */
    public function UserAddressDelete($id)
    {   
        $userInfo=$this->currentUser;//登录信息
        $res = $this->model
        ->where("id",$id)
        ->where("user_id",$userInfo['user_id'])
        ->field("id")
        ->find();

        if (!isset($res['id'])) {
            return self::returnMsg(0,[],'数据不存在');
        }
        $flag = $this->model
        ->where("id",$id)
        ->where("user_id",$userInfo['user_id'])
        ->delete();
        return self::returnMsg($flag?1:0);
    }

    /**
     * 用户信息修改添加
     * @param request
     * @return \think\response\Json
     */
    public function UserAddressSave(Request $request){

        $formData=$request->param();
        $userInfo=$this->currentUser;//登录信息
        //添加
        $data['user_id']=$userInfo['user_id'];
        $data['take_phone']=$formData['info']['take_phone'];
        $data['take_name']=$formData['info']['take_name'];
        $data['province']=$formData['info']['province'];
        $data['city']=$formData['info']['city'];
        $data['district']=$formData['info']['district'];
        $data['detail_address']=$formData['info']['detail_address'];
        $data['is_default']=$formData['info']['is_default']?1:2;
        $validate = 'app\validate\UserAddressValidate.add';
        $result = $this->validate($data,$validate);
        if (true!==$result) {
            return self::returnMsg(0,[],$result);
        }

        $address=$data['province'].$data['city'].$data['district'].$data['detail_address'];
        $key=Config::get("system.qq_map.key");
        $url="https://apis.map.qq.com/ws/geocoder/v1/?key=$key&address=$address";//地址转坐标
        $addressInfo=json_decode(Util::httpGet($url),true);
        if ($addressInfo['status']!=0) {
            return self::returnMsg(0,[],$addressInfo['message']);
        }
        $data['lng']=$addressInfo['result']['location']['lng'];
        $data['lat']=$addressInfo['result']['location']['lat'];
        if ($formData['type']=="add") {
            $res=$this->model->insert($data);
        }
        if ($formData['type']=="edit") {
            //添加
            $data['id']=$formData['info']['id'];

            $res=$this->model
            ->where("id",$data['id'])
            ->where("user_id",$data['user_id'])
            ->field("id")
            ->find();
            if (!isset($res['id'])) {
                return self::returnMsg(0,[],"地址信息不存在");
            }
            if ($data['is_default']==1) {
                //设置默认
                $up['is_default']=2;
                $res=$this->model
                ->where("user_id",$data['user_id'])
                ->update($up);
            }
            $res=$this->model->update($data);
        }

        return self::returnMsg(1);
    }
}
